home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter4 / cupghwnd.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  8KB  |  301 lines

  1.  
  2. #include <windows.h>  
  3. #include "CuPgHwnd.h"  
  4. #include "commctrl.h"
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. HWND    hPropSheet  = NULL;
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "PropSheet_GetCurrentPageHwnd"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       if ( IsWindow( hPropSheet ) && PropSheet_IsDialogMessage( hPropSheet, &msg ) )
  75.          continue;
  76.  
  77.       TranslateMessage( &msg ); 
  78.       DispatchMessage( &msg );  
  79.    }
  80.  
  81.    return( msg.wParam ); 
  82. }
  83.  
  84.  
  85. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  86. {
  87.    WNDCLASSEX wcex;
  88.  
  89.    wcex.style         = lpwc->style;
  90.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  91.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  92.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  93.    wcex.hInstance     = lpwc->hInstance;
  94.    wcex.hIcon         = lpwc->hIcon;
  95.    wcex.hCursor       = lpwc->hCursor;
  96.    wcex.hbrBackground = lpwc->hbrBackground;
  97.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  98.    wcex.lpszClassName = lpwc->lpszClassName;
  99.  
  100.    // Added elements for Windows 95.
  101.    //...............................
  102.    wcex.cbSize = sizeof(WNDCLASSEX);
  103.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  104.                             IMAGE_ICON, 16, 16,
  105.                             LR_DEFAULTCOLOR );
  106.             
  107.    return RegisterClassEx( &wcex );
  108. }
  109.  
  110.  
  111.  
  112. LRESULT CALLBACK Page1Proc( HWND hDlg, UINT message,        
  113.                             WPARAM wParam, LPARAM lParam )
  114. {
  115.    switch (message) 
  116.    {
  117.        case WM_INITDIALOG : 
  118.                return (TRUE);
  119.  
  120.        case WM_USER :
  121.                MessageBox( hDlg, "Message received by Page 1.",
  122.                                  lpszTitle, MB_OK | MB_ICONINFORMATION ); 
  123.                break;
  124.  
  125.        case WM_NOTIFY :
  126.                switch( ((NMHDR*)lParam)->code )
  127.                {
  128.                   case PSN_APPLY :
  129.                   case PSN_QUERYCANCEL :
  130.                          DestroyWindow( hPropSheet );
  131.                          break;
  132.                }
  133.                break;
  134.    }
  135.  
  136.    return( FALSE );
  137. }
  138.  
  139.  
  140. LRESULT CALLBACK Page2Proc( HWND hDlg, UINT message,        
  141.                             WPARAM wParam, LPARAM lParam )
  142. {
  143.    switch (message) 
  144.    {
  145.        case WM_INITDIALOG : 
  146.                return (TRUE);
  147.  
  148.        case WM_USER :
  149.                MessageBox( hDlg, "Message received by Page 2.",
  150.                                  lpszTitle, MB_OK | MB_ICONINFORMATION ); 
  151.                break;
  152.  
  153.        case WM_NOTIFY :
  154.                switch( ((NMHDR*)lParam)->code )
  155.                {
  156.                   case PSN_APPLY :
  157.                   case PSN_QUERYCANCEL :
  158.                          DestroyWindow( hPropSheet );
  159.                          break;
  160.                }
  161.                break;
  162.    }
  163.  
  164.    return( FALSE );
  165. }
  166.  
  167.  
  168. LRESULT CALLBACK Page3Proc( HWND hDlg, UINT message,        
  169.                             WPARAM wParam, LPARAM lParam )
  170. {
  171.    switch (message) 
  172.    {
  173.        case WM_INITDIALOG : 
  174.                return (TRUE);
  175.  
  176.        case WM_USER :
  177.                MessageBox( hDlg, "Message received by Page 3.",
  178.                                  lpszTitle, MB_OK | MB_ICONINFORMATION ); 
  179.                break;
  180.  
  181.        case WM_NOTIFY :
  182.                switch( ((NMHDR*)lParam)->code )
  183.                {
  184.                   case PSN_APPLY :
  185.                   case PSN_QUERYCANCEL :
  186.                          DestroyWindow( hPropSheet );
  187.                          break;
  188.                }
  189.                break;
  190.    }
  191.  
  192.    return( FALSE );
  193. }
  194.  
  195.  
  196. VOID DisplayProperties( HWND hWnd )
  197. {
  198.    PROPSHEETPAGE   psp;
  199.    PROPSHEETHEADER psh;
  200.    HPROPSHEETPAGE  hpsp[3];
  201.  
  202.    psp.dwSize    = sizeof( PROPSHEETPAGE );
  203.    psp.dwFlags   = PSP_DEFAULT;
  204.    psp.hInstance = hInst;
  205.  
  206.    psp.pszTemplate = "PAGE1";
  207.    psp.pfnDlgProc  = (DLGPROC)Page1Proc;
  208.    hpsp[0] = CreatePropertySheetPage( &psp );
  209.  
  210.    psp.pszTemplate = "PAGE2";
  211.    psp.pfnDlgProc  = (DLGPROC)Page2Proc;
  212.    hpsp[1] = CreatePropertySheetPage( &psp );
  213.  
  214.    psp.pszTemplate = "PAGE3";
  215.    psp.pfnDlgProc  = (DLGPROC)Page3Proc;
  216.    hpsp[2] = CreatePropertySheetPage( &psp );
  217.  
  218.    // Initialize the property sheet structure.
  219.    //.........................................
  220.    memset( &psh, 0, sizeof( PROPSHEETHEADER ) );
  221.    psh.dwSize     = sizeof( PROPSHEETHEADER );
  222.    psh.dwFlags    = PSH_USEICONID | PSH_MODELESS | PSH_NOAPPLYNOW;
  223.    psh.hInstance  = hInst;
  224.    psh.hwndParent = hWnd;
  225.    psh.pszIcon    = "SMALL";
  226.    psh.nPages     = 3;
  227.    psh.phpage     = hpsp;
  228.    psh.pszCaption = "Properties";
  229.  
  230.    // Create a modeless property sheet.
  231.    //..................................
  232.    hPropSheet = (HWND)PropertySheet( &psh );
  233. }
  234.  
  235.  
  236. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  237. {
  238.    switch( uMsg )
  239.    {
  240.       case WM_CREATE :
  241.               InitCommonControls();
  242.               DisplayProperties( hWnd );
  243.               break;
  244.  
  245.       case WM_COMMAND :
  246.               switch( LOWORD( wParam ) )
  247.               {
  248.                  case IDM_TEST :
  249.                         if ( IsWindow( hPropSheet ) )
  250.                         {
  251.                            HWND hPage = PropSheet_GetCurrentPageHwnd( hPropSheet );
  252.  
  253.                            SendMessage( hPage, WM_USER, 0, 0 );
  254.                         }
  255.                         break;
  256.  
  257.                  case IDM_ABOUT :
  258.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  259.                         break;
  260.  
  261.                  case IDM_EXIT :
  262.                         DestroyWindow( hWnd );
  263.                         break;
  264.               }
  265.               break;
  266.       
  267.       case WM_DESTROY :
  268.               PostQuitMessage(0);
  269.               break;
  270.  
  271.       default :
  272.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  273.    }
  274.  
  275.    return( 0L );
  276. }
  277.  
  278.  
  279. LRESULT CALLBACK About( HWND hDlg,           
  280.                         UINT message,        
  281.                         WPARAM wParam,       
  282.                         LPARAM lParam)
  283. {
  284.    switch (message) 
  285.    {
  286.        case WM_INITDIALOG: 
  287.                return (TRUE);
  288.  
  289.        case WM_COMMAND:                              
  290.                if (   LOWORD(wParam) == IDOK         
  291.                    || LOWORD(wParam) == IDCANCEL)    
  292.                {
  293.                        EndDialog(hDlg, TRUE);        
  294.                        return (TRUE);
  295.                }
  296.                break;
  297.    }
  298.  
  299.    return (FALSE); 
  300. }
  301.